/**
 * mobileMonitor vuex
 */
import posts from '@/plugins/request'
import common from '@/plugins/common'

// initial state
const state = {
  researchAnalysisData: [],
  reportFormsData: {
    data: [],
    total: 0
  },
  mobileMonitorData: {
    data: [],
    total: 0,
    curDetail: [],
    curDetailPhoto: [],
  }
}
// getters
const getters = {
  getAnalysisData: state => state.researchAnalysisData,
  getReportFormsData: state => state.reportFormsData,
  getMobileData: state => state.mobileMonitorData,
}
// actions
const actions = {
  async getResearchAnalysis({ commit }, param) {
    let data = await posts.postResearchAnalysis(param)
    if(param.isExcel === 'yes') {
      // window.open(data)
    }else {
      for(let i = 0; i < data.length; i++) {
        let achieve = 1 - data[i].exessRate
        data[i].achieve = achieve
      }
      commit('changeResearchAnalysisData', data)
    }
  },
  async downAnalysisExcel({commit}, param) {
    await posts.postResearchAnalysisExcel(param)
  },
  async getReportForms({ commit }, param) {
    let data = await posts.postReportFormsData(param)
    commit('changeReportFormsData', data)
  },
  async downReportFormsExcel({ commit }, param) {
    await posts.postReportFormsDataExcel(param)
  },
  async getMobileMonitorData({ commit }, param) {
    let data = await posts.postMobileMonitorData(param)
    let rows = data.rows
    for(let i = 0; i < rows.length; i++) {
      rows[i].overFactor = common.translate(rows[i].overFactor)
    }

    commit('changeMobileData', data)
  },
  async uploadMobileLine({ commit }, param) {
    let res = await posts.postMobileMonitorLineInput(param)
    return res
  },
  async uploadMobileMonitor({ commit }, param) {
    let res = await posts.postMobileMonitorInput(param)
    return res
  },
  
}

// mutations
const mutations = {
  changeResearchAnalysisData(state, data) {
    state.researchAnalysisData = data
  },
  changeReportFormsData(state, data) {
    state.reportFormsData.data = data.data
    state.reportFormsData.total = data.totalpage
  },
  changeMobileData(state, data) {
    state.mobileMonitorData.data = data.rows
    state.mobileMonitorData.total = data.total
  },
  changeMobileDataDetail(state, data) {
    state.mobileMonitorData.curDetail = data
    state.mobileMonitorData.curDetailPhoto = data.map(v => {
      return {
        name: v.pollutantName,
        img: v.photo.substring(0, v.photo.length - 1)
      }
    })
  }
}
export default {
  state,
  getters,
  actions,
  mutations
}